package com.example.capstome;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;
import java.util.Date;


public class DB_Helper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "TikTok.db";
    private static final int DATABASE_VERSION = 1;

    private static final String SQL_CREATE_ENTRY_COMMENT =
            "CREATE TABLE " + DB_Common_Contract.CommentEntry.TABLE_NAME + " (" +
                    DB_Common_Contract.CommentEntry._ID + " INTEGER PRIMARY KEY," +
                    DB_Common_Contract.CommentEntry.COLUMN_COMMENT_TEXT + " TEXT, "+
                    DB_Common_Contract.CommentEntry.COLUMN_COMMENT_SUPPORT + " INTEGER, "+
                    DB_Common_Contract.CommentEntry.COLUMN_COMMENT_UserID + " TEXT, "+
                    DB_Common_Contract.CommentEntry.COLUMN_COMMENT_TIME + " TEXT)";

    private static final String SQL_DELETE_ENTRIES_COMMENT =
            "DROP TABLE IF EXISTS " + DB_Common_Contract.CommentEntry.TABLE_NAME;

    private static final String SQL_CREATE_ENTRY_USER =
            "CREATE TABLE " + DB_User_Contract.UserEntry.TABLE_NAME + " (" +
                    DB_User_Contract.UserEntry._ID + " INTEGER PRIMARY KEY," +
                    DB_User_Contract.UserEntry.COLUMN_USER_NAME + " TEXT, "+
                    DB_User_Contract.UserEntry.COLUMN_USER_ImgURL + " TEXT)";

    private static final String SQL_DELETE_ENTRIES_USER =
            "DROP TABLE IF EXISTS " + DB_User_Contract.UserEntry.TABLE_NAME;

    public DB_Helper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_ENTRY_COMMENT);
        db.execSQL(SQL_CREATE_ENTRY_USER);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_ENTRIES_COMMENT);
        onCreate(db);
    }

    public Cursor QueryDB(String query, SQLiteDatabase db){
        return db.rawQuery(query, null);
    }

    public void addSupport(String id, Integer newValue) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_SUPPORT, newValue);

        String selection = "_id=" + id;

        db.update("comments", values, selection, null);
        db.close();
    }

    public void addComment(String comment_text){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_TEXT,comment_text);
        values.put(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_SUPPORT, 0);
        values.put(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_UserID, Util.UserID);
        Date date = new Date();
        String time = Util.sdf.format(date);
        values.put(DB_Common_Contract.CommentEntry.COLUMN_COMMENT_TIME, time);

        db.insert(DB_Common_Contract.CommentEntry.TABLE_NAME,null, values);
        db.close();
    }

    public void delComment(ArrayList<String> id){
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction(); // 开始事务
        try {
            for (String s : id) {
                db.delete(DB_Common_Contract.CommentEntry.TABLE_NAME, "id = ?", new String[] {s});
            }
            db.setTransactionSuccessful(); // 标记事务成功
        } catch (Exception e) {
            Log.e("DBHelper", "Transaction failed: " + e.getMessage());
        } finally {
            db.endTransaction(); // 结束事务
        }
        db.close();
    }

    public void addUser(User user){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(DB_User_Contract.UserEntry.COLUMN_USER_NAME,user.name);
        values.put(DB_User_Contract.UserEntry.COLUMN_USER_ImgURL,user.avatarURL);

        db.insert(DB_User_Contract.UserEntry.TABLE_NAME,null, values);
        db.close();
    }

    public void delUser(ArrayList<String> id){
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction(); // 开始事务
        try {
            for (String s : id) {
                db.delete(DB_User_Contract.UserEntry.TABLE_NAME, "id = ?", new String[] {s});
            }
            db.setTransactionSuccessful(); // 标记事务成功
        } catch (Exception e) {
            Log.e("DBHelper", "Transaction failed: " + e.getMessage());
        } finally {
            db.endTransaction(); // 结束事务
        }
        db.close();
    }

}

